#include <QDebug>
#include <QCoreApplication>
#include <iostream>
#include <QVariant>

template <typename Type>
bool setVar(QVariantList &data,int index,Type &var) {
    if(data.count() > index){
        var = data.at(index);
        return true;
    }
    return false;
}
template <typename Type, typename... Targs>
bool setVar(QVariantList &data,int index,Type &var, Targs &...args) {
    if(!setVar(data,index,var))
        return false;
    return setVar(data,++index,args...);
}

template <typename Type>
void getVar(QVariantList &data,Type &var) {
    data.append(var);
}
template <typename Type, typename... Targs>
void getVar(QVariantList &data,Type &var, Targs &...args) {
    getVar(data,var);
    getVar(data,args...);
}

/*
该宏实现三个功能
获取表的所有列名，与变量名称同名，且首列为主键
从 QVariantList 加载值到结构体的每个成员变量中
把所有成员变量返回为 QVariantList
*/
#define ROW_DATA_HELPER(...)                  \
bool load(QVariantList &data){                  \
    return setVar(data,0,__VA_ARGS__);          \
}                                               \
void data(QVariantList &data){                  \
    getVar(data,__VA_ARGS__);                   \
}                                               \
QStringList colNames(){                         \
     return QString(#__VA_ARGS__).split(',');   \
}
//定义表名 整表创建、删除、查询语句，主键单行添加，删除，修改，查询

#define TABLE_HEARDER_HELPER()
struct RowHistory{
    QVariant id = "";           //根据初值定义列类型和列名称
    QVariant date = 0;
    QVariant info = true;
    QVariant message;
    ROW_DATA_HELPER(id,date,info,message)
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QVariantList data;
    data<<QVariant(320)<<"20220123"<<"debug"<<"hello world";
    qDebug()<<"load:"<<data;

    QVariantList exportData;

    RowHistory st;
    st.data(exportData);
    qDebug()<<"export:"<<exportData;


    bool loadOk = st.load(data);
    qDebug()<<loadOk<<"St info:"<<st.id<<st.date<<st.info<<st.message;


    st.data(exportData);
    qDebug()<<"export:"<<exportData;
    qDebug()<<"colNames:"<<st.colNames();
    return a.exec();
}
